﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentManage
{
    public partial class FrmAddStudent : Form
    {
        public FrmAddStudent()
        {
            InitializeComponent();
        }       

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAddStudent_Load(object sender, EventArgs e)
        {
            InitClasses();//初始化班级信息
            rbtMale.Checked = true;//默认选中：男

        }

        private void InitClasses()
        {
            string sqlClass = "SELECT c.ClassId,g.GradeName,c.ClassName " +
                               "FROM gradeinfo g,classinfo c WHERE g.GradeId = c.GradeId";
            DataTable dtClassList = SqlHelper.GetDataTable(sqlClass);
            if (dtClassList.Rows.Count > 0)
            {
                foreach (DataRow row in dtClassList.Rows)
                {
                    string className = row["ClassName"].ToString();
                    string gradeName = row["GradeName"].ToString();
                    row["ClassName"] = className + "--" + gradeName;
                }
            }
            //设置数据源
            cboClasses.DataSource = dtClassList;
            cboClasses.DisplayMember = "ClassName";
            cboClasses.ValueMember = "ClassId";
            //默认选中第一行
            cboClasses.SelectedIndex = 0;
        }

        /// <summary>
        /// 添加学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddStu_Click(object sender, EventArgs e)
        {
            //获取页面输入信息
            string stuName = txtStuName.Text.Trim();
            int classId = (int)cboClasses.SelectedValue;
            string sex = rbtMale.Checked ? rbtMale.Text.Trim() : rbtFemale.Text.Trim();
            string phone = txtPhone.Text.Trim();
            //判断信息是否为空: 姓名+电话不可以为空
            if (string.IsNullOrEmpty(stuName))
            {
                MessageBox.Show("姓名不能为空", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(phone))
            {
                MessageBox.Show("电话不能为空", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //判断学生是否已经存在： 姓名+电话 
            string sqlExists = "SELECT COUNT(1) FROM studentinfo " +
                               "WHERE StuName=@StuName AND Phone=@Phone";
            MySqlParameter[] paraExists =
            {
                new MySqlParameter("@StuName",stuName),
                new MySqlParameter("@Phone",phone)
            };
            object result = SqlHelper.ExecuteScalar(sqlExists,1,paraExists);
            if ((long)result != 0)
            {
                MessageBox.Show("该学生已存在", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //不存在，添加入库操作
            string sqlAdd = "insert into studentInfo(StuName,ClassId,Sex,Phone,CreateTime)" +
                            "values(@StuName,@ClassId,@Sex,@Phone,@CreateTime)";
            //参数设置
            MySqlParameter[] paraAdd =
            {
                    new MySqlParameter("@StuName",stuName),
                    new MySqlParameter("@ClassId",classId),
                    new MySqlParameter("@Sex",sex),
                    new MySqlParameter("@Phone",phone),
                    new MySqlParameter("@CreateTime",DateTime.Now.ToString("yyyy-MM-dd"))
                };
            //执行
            int rs = SqlHelper.ExecuteNonQuery(sqlAdd, paraAdd);
            if (rs > 0)
            {
                MessageBox.Show($"学生:{ stuName}添加成功", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("学生添加失败", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
